const express = require('express');
const mongoose = require('mongoose');
// 链接mongo
const DB_URl = "mongodb://127.0.0.1:27017/imooc";
mongoose.connect(DB_URl)
const db = mongoose.connection;
db.on("connected", function() {
    console.log("mongo cennect sucess")
})

//类似与mysq的表，mongo里有文档，字段的概念

const User = mongoose.model('user',
        new mongoose.Schema({
            name: {
                type: String,
                require: true
            },
            age: {
                type: Number,
                require: true
            }
        })
    )
    // User.remove({
    //         age: 43
    //     }, function(err, doc) {
    //         console.log(err, 111, doc)
    //     })
    //新增数据
    // User.create({
    //     name: "sunyana",
    //     age: Math.ceil(Math.random() * 100)
    // }, function(err, doc) {
    //     console.log(doc)
    // })
User.update({
        age: 89,
    }, {
        '$set': {
            name: 'ganjiqng'
        }
    },
    function(err, doc) {
        console.log(err, 11)
        console.log(doc)
    }
)

//新建app
const app = express();

app.get('/', function(req, res) {
    res.send('hello node')
})
app.get('/data', function(req, res) {
    User.find({}, function(err, doc) {
        res.json(doc)
    })

})
app.listen(8888, function() {
    console.log('Node app start at port 8888')
});